/*
 * @(#)XfdmFuzzyCN2DataSet.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.xfdm.model.algorithm;

import xfuzzy.lang.*;
import xfuzzy.xfds.XfdsDataSet;


/**
 * Clase que describe un conjunto de datos "fuzzificados"
 * 
 * @author Pablo Mart�n Mu�oz
 *
 */
public class XfdmFuzzyCN2DataSet {

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Descripci�n difusa de los atributos de entrada
	 */
	private Type[] attrib;
	
	/**
	 * Descripci�n difusa del atributo de clasificaci�n
	 */
	private Type classif;

	//----------------------------------------------------------------------------//
	//                            MIEMBROS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Grado de certeza de cada patr�n de datos
	 */
	public double[] certainty;
	
	/**
	 * Valores de entrada (atributos de entrada)
	 */
	public double[][] input;
	
	/**
	 * Valores de salida (atributo de clasificacion)
	 */
	public int[] output;

	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor basado en un conjunto de patrones "crisp"
	 */
	public XfdmFuzzyCN2DataSet(XfdsDataSet pattern, Type[] attrib, Type classif) {
		this.attrib = attrib;
		this.classif = classif;
		
		LinguisticLabel mf[] = classif.getAllMembershipFunctions();
		double[] label = new double[mf.length];
		for(int i=0; i<mf.length; i++) label[i] = mf[i].get()[0];


		int numpatterns = pattern.input.length;
		this.certainty = new double[numpatterns];
		for(int i=0; i<numpatterns; i++) this.certainty[i] = 1.0;
		
		this.input = new double[numpatterns][attrib.length];
		for(int i=0; i<attrib.length; i++) {
			for(int p = 0; p<numpatterns; p++) {
				input[p][i] = pattern.input[p][i];
			}
		}
		
		this.output = new int[numpatterns];
		for(int p=0; p<numpatterns; p++) {
			for(int i=0; i<label.length; i++) {
				if(pattern.output[p][0] == label[i]) output[p] = i;
			}
			// output[p] = (int) Math.round(pattern.output[p][0]);
		}
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Obtiene la descripci�n difusa de los atibutos de entrada
	 */
	public Type[] getAttrib() {
		return this.attrib;
	}
	
	/**
	 * Obtiene la descripci�n difusa del atributo de clasificaci�n
	 */
	public Type getClassif() {
		return this.classif;
	}

	/**
	 * Comprueba si todas las instancias tienen grado de certeza cero
	 */
	public boolean isEmpty() {
		int i = 0;
		boolean allzeros = true;
		while((i<this.certainty.length) && (allzeros)) {
			if(this.certainty[i] != 0) {
				allzeros = false;
			}
			i++;
		}
		return allzeros;
	}
	
}